<template>
  <div class="login">
    <div class="title">
      <h1>跃迁</h1>
    </div>
    <div class="loginbox">
      <div class="boxtitle">
        分销系统
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
        <el-form-item prop="username" class="loginForm-item">
          <div class="form-item">
            <div class="item-icon">
              <img src="./image/user.png" alt="">
            </div>
            <el-input name="username" type="text" v-model="loginForm.username" autoComplete="off"></el-input>
          </div>
        </el-form-item>
        <el-form-item prop="password" class="loginForm-item">
          <div class="form-item">
            <div class="item-icon">
              <img src="./image/password.png" alt="">
            </div>
            <el-input name="password" @keyup.enter.native="handleLogin" :type="passwordType" v-model="loginForm.password" auto-complete="off"></el-input>
            <img src="./image/eye.png" class="passshow" @click="showPassword" alt="">
          </div>
        </el-form-item>
        <el-form-item>
          <div class="form-item">
            <div class="item-icon">
              <img src="./image/project.png" alt="">
            </div>
            <el-select v-model="loginForm.project">
              <el-option v-for="item in projectList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="form-item">
            <el-button :loading="logining" type="primary" @click.native.prevent="handleLogin">登录</el-button>
            <el-button @click="clearForm">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {isvalidUsername} from '../../utils/validate.js'
import {loginByUsername} from '../../api/login.js'
export default {
  data () {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('用户名必须是英文字母，数字或下划线组合'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码必须大于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        project: 'lemon'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      projectList: [
        {
          label: '柠檬记账',
          value: 'lemon'
        }
      ],
      passwordType: 'password',
      logining: false
    }
  },
  methods: {
    showPassword () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.logining = true
          // 调用api文件登陆接口
          loginByUsername(this.loginForm).then(res => {
            this.logining = false
            let {code, msg, token} = res.data
            // 根据返回的code判断请求是否成功
            if (code === 200) {
              this.$message({
                type: 'success',
                message: msg
              })
              // 登陆成功,用户信息保存在sessionStorage中
              this.$store.commit('SET_TOKEN', token)
              window.sessionStorage.setItem('token', token)
              this.$router.push({path: '/'})
            } else {
              this.$message({
                type: 'error',
                message: msg
              })
            }
          }).catch(err => {
            this.logining = false
            console.log(err)
          })
        }
      })
    },
    clearForm () {
      this.loginForm = {
        username: '',
        password: '',
        project: 'lemon'
      }
    }
  }
}
</script>

<style lang="less">
.loginForm-item{
  .el-form-item__error{
    left: 60px;
  }
}
</style>

<style lang="less" scoped>
.login{
  margin: 0 auto;
  text-align: center;
  width: 100%;
  height: 100%;
  background: rgb(240, 242, 245);
  .title{
    padding: 20px;
    background: #fff;
  }
  .loginbox{
    border: 1px solid #ccc;
    background: #fff;
    border-radius: 10px;
    width: 400px;
    margin: 20px auto;
    .boxtitle{
      padding: 20px;
      font-size: 18px;
    }
    .el-form{
      padding: 10px;
      border-top: 1px solid #ccc;
      .form-item{
        display: flex;
        justify-content: center;
        .item-icon{
          display: flex;
          align-items: center;
          margin: 10px;
        }
      }
      .el-form-item{
        margin: 20px;
        .el-form-item__content{
          display: flex;
          flex-direction: column;
          justify-content: center;
        }
      }
      .el-input,.el-select{
        width: 280px !important;
        height: 32px !important;
      }
      .passshow{
        position: absolute;
        right: 20px;
        top: 8px;
        cursor: pointer;
      }
    }
  }
}
</style>
